package cn.ronghuanet.gift.service.impl;

import cn.ronghuanet.gift.domain.Spu;
import cn.ronghuanet.gift.domain.SpuOnsaleDetail;
import cn.ronghuanet.gift.mapper.SpuMapper;
import cn.ronghuanet.gift.mapper.SpuOnsaleDetailMapper;
import cn.ronghuanet.gift.service.ISpuService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * spu表 服务实现类
 * </p>
 *
 * @author ysosang
 * @since 2022-12-19
 */
@Service
public class SpuServiceImpl extends ServiceImpl<SpuMapper, Spu> implements ISpuService {

    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private SpuOnsaleDetailMapper onsaleDetailMapper;
    //  在上下架时不涉及任何审核相关的   是在添加或修改的保存是做百度人工智能审核!   只需要做一件事,上架时判断是否是已经审核通过的
    @Override
    public void onsale(List<Long> ids) {
        //参数校验
        if(ids == null || ids.size() == 0){//没有值
            throw new RuntimeException("请选中数据后操作");
        }

        //1 判断要上架的产品状态是否为审核
        int i = spuMapper.stateByids(ids);
        if(ids.size() > i){
            throw new RuntimeException("请检查选中的数据是否已经全部审核通过");
        }
        //2 修改状态
        spuMapper.onsele(ids);
        //3 添加上架详情数据,以便于canal同步数据
        for (Long id: ids) {
            Spu spu = spuMapper.selectById(id);
            SpuOnsaleDetail detail = new SpuOnsaleDetail();
            detail.setMaxPrice(spu.getHighPrice());
            detail.setMinPrice(spu.getLowPrice());
            detail.setName(spu.getGoodsName());
            detail.setTenantId(spu.getTenantId());
            detail.setSpuId(spu.getId());
            onsaleDetailMapper.insert(detail);
        }
        //4 添加上下架日志 @TODO
    }

    @Override
    public void offsale(List<Long> ids) {
        //2 修改数据库下架状态
        //参数校验
        if(ids == null || ids.size() == 0){//没有值
            throw new RuntimeException("请选中数据后操作");
        }
        //下架
        spuMapper.offsele(ids);
        //3 删除下架详情数据,以便于canal同步数据
        onsaleDetailMapper.deleteBySpuId(ids);
        //4 添加上下架日志 @TODO

    }
}
